program nc_reducer

    use iasing_osse
    use netcdf

    implicit none
    ! Program counters 
    integer :: iowrite
    integer :: jo
    integer :: nobs
    integer, allocatable :: listObs(:)

    ! Netcdf stuff
    character(len=64) :: ncfile 
    integer :: ncid, ouid

    ! iasing_osse stuff
    type(r4a_ids) :: db_ids, ou_ids
    type(r60_dat) :: db_dat, ou_rec

    ! Interfaces
    include "checknc.interface"
    include "r60_initializer.interface"
    include "r60_reader.interface"
    include "r60_writer.interface"
    include "r60_idscreator.interface"

    ! Read selected
    open(1,file='selected',status='old',action='read')
    read(1,'(A)') ncfile
    read(1,*) nobs
    allocate(listObs(nobs))
    read(1,*) listObs
    close (1)

    ! Create new file
    call checknc(nf90_create("example.nc", NF90_HDF5, ouid))
    ou_ids = r60_idscreator(ouid, nobs)
    call checknc(nf90_enddef(ouid))

    ! Print info
    print*, ncfile

    ! Open old netcdf Databank
    call checknc(nf90_open(ncfile, NF90_NOWRITE, ncid))
    db_ids = r60_initializer(ncid)

    do jo = 1, nobs
        db_dat  = r60_reader(ncid, db_ids, listObs(jo))
        iowrite = r60_writer(ouid, ou_ids, jo, db_ids, db_dat)
        if (nobs>10.and.mod(jo,nobs/10).eq.0) print*, "Observation ", jo
    enddo

    ! Close Netcdf Databank
    call checknc(nf90_close(ncid))
    call checknc(nf90_close(ouid))

end program
